<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Contexts</title>
<link rel="stylesheet" href="../../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Boost.Test">
<link rel="up" href="../test_tools_support_for_logging.html" title="Tools supports for logging">
<link rel="prev" href="checkpoints.html" title="Checkpoints for accurate failure location">
<link rel="next" href="log_floating_points.html" title="Logging floating point type numbers">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="checkpoints.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../test_tools_support_for_logging.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="log_floating_points.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_test.test_output.test_tools_support_for_logging.contexts"></a><a class="link" href="contexts.html" title="Contexts">Contexts</a>
</h4></div></div></div>
<p>
          Contexts are a facility provided by the <span class="emphasis"><em>Unit Test Framework</em></span>
          in order to be able to trace the location of assertions better. To grasp
          the idea, consider the following example:
        </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">Processor</span><span class="special">&amp;</span> <span class="identifier">processor</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">limit</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">limit</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">));</span>
    <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">++</span><span class="identifier">j</span><span class="special">)</span> <span class="special">{</span>
      <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">));</span>
    <span class="special">}</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
          In case of failure, in order to see in the logs at which point of the loops
          the failure occurred, we need some extra information in the assertion,
          which can be achieved for instance <a class="link" href="../../testing_tools/reports.html#boost_test.testing_tools.reports.custom_messages">the
          following way</a>:
        </p>
<pre class="programlisting"><span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">));</span>
</pre>
<p>
          replaced by
        </p>
<pre class="programlisting"><span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">),</span> <span class="string">"With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">);</span>
</pre>
<p>
          We see in this trivial example that a context, which is the variable <code class="computeroutput"><span class="identifier">i</span></code> in this case, should be acknowledged
          by the assertion <code class="computeroutput"><span class="identifier">BOOST_CHECK</span></code>
          in a particular way. In the approach above, this is done by adding a message
          to the assertion itself.
        </p>
<p>
          What if the context is more complex than that? In case the complexity of
          the context increases, the fact that the assertion and the context is tightly
          coupled as in the approach above is difficult to maintain:
        </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">Processor</span><span class="special">&amp;</span> <span class="identifier">processor</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">limit</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">level</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">limit</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">),</span>
               <span class="string">"With optimization level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span> <span class="special">&lt;&lt;</span> <span class="string">", With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">);</span>
    <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">++</span><span class="identifier">j</span><span class="special">)</span> <span class="special">{</span>
      <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">),</span>
                 <span class="string">"With optimization level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span> <span class="special">&lt;&lt;</span>
                 <span class="string">", With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">&lt;&lt;</span> <span class="string">", With parameter j = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">j</span><span class="special">);</span>
    <span class="special">}</span>
  <span class="special">}</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test1</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">Processor</span> <span class="identifier">processor</span><span class="special">;</span>

  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">level</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">3</span><span class="special">;</span> <span class="special">++</span><span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">processor</span><span class="special">.</span><span class="identifier">optimization_level</span><span class="special">(</span><span class="identifier">level</span><span class="special">);</span>
    <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">processor</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">level</span><span class="special">);</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
          Note the length of the messages, the repetition, and the fact, that we
          pass argument <code class="computeroutput"><span class="identifier">level</span></code> to
          function <code class="computeroutput"><span class="identifier">test_operations</span></code>
          only for the sake of generating an error message in case of a failure.
        </p>
<p>
          Therefore, <span class="bold"><strong>loose</strong></span> coupling between the
          context of an assertion and the assertion point is a property that is desirable.
        </p>
<a name="ref_BOOST_TEST_INFO"></a><h4>
<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h0"></a>
          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.assertion_bound_context"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.assertion_bound_context">Assertion-bound
          context</a>
        </h4>
<p>
          <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO</span></code> can be
          used to define an error message to be bound to the first following assertion.
          If (and only if) the assertion fails, the bound message will be displayed
          along:
        </p>
<h6>
<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h1"></a>
          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.example_descr"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.example_descr">Example:
          Assertion-bound context</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example80</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">void</span> <span class="identifier">test</span><span class="special">()</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test_case1</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"Alpha"</span><span class="special">);</span>
  <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"Beta"</span><span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="keyword">true</span><span class="special">);</span>

  <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"Gamma"</span><span class="special">);</span>
  <span class="keyword">char</span> <span class="identifier">a</span> <span class="special">=</span> <span class="char">'a'</span><span class="special">;</span>
  <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"Delt"</span> <span class="special">&lt;&lt;</span> <span class="identifier">a</span><span class="special">);</span>
  <span class="identifier">test</span><span class="special">();</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span>
<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">14</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_case1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="keyword">false</span> <span class="identifier">has</span> <span class="identifier">failed</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">Gamma</span>
    <span class="identifier">Delta</span>

<span class="special">***</span> <span class="number">1</span> <span class="identifier">failures</span> <span class="identifier">is</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example80"</span>
</pre>
                </td></tr></tbody>
</table></div>
<p>
          The information composed inside <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO</span></code>
          is bound only to the first assertion following the declaration. This information
          is only displayed if the assertion fails; otherwise the message is discarded.
          The <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO</span></code> declaration
          does not have to immediately precede the assertion, it is allowed to intertwine
          them with other instructions, they can even be declared in different scopes.
          It is also possible to bind more than one information to a given assertion.
        </p>
<p>
          With <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO</span></code>, we
          can improve our initial example as follows:
        </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">Processor</span><span class="special">&amp;</span> <span class="identifier">processor</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">limit</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">level</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">limit</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With optimization level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span><span class="special">);</span>
    <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">);</span>
    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">));</span>
    <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">++</span><span class="identifier">j</span><span class="special">)</span> <span class="special">{</span>
      <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With optimization level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span><span class="special">);</span>
      <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">);</span>
      <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter j = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">j</span><span class="special">);</span>
      <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">));</span>
    <span class="special">}</span>
  <span class="special">}</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test1</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">Processor</span> <span class="identifier">processor</span><span class="special">;</span>

  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">level</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">3</span><span class="special">;</span> <span class="special">++</span><span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">processor</span><span class="special">.</span><span class="identifier">optimization_level</span><span class="special">(</span><span class="identifier">level</span><span class="special">);</span>
    <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">processor</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="identifier">level</span><span class="special">);</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
<a name="ref_BOOST_TEST_CONTEXT"></a><h4>
<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h2"></a>
          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.scope_bound_context"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.scope_bound_context">Scope-bound
          context</a>
        </h4>
<p>
          In the previous example, the information stored inside the calls to <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO</span></code> were all consumed by
          the next assertion. There are cases where we would like this information
          be persistent for the current scope. <span class="emphasis"><em>Unit Test Framework</em></span>
          provides two tools to achieve this:
        </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
              <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code>
              defines a diagnostic message and a scope. The message is bound to every
              assertion in that scope, and is displayed along with every failed assertion.
            </li>
<li class="listitem">
              <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO_SCOPE</span></code>
              acts the same as <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO</span></code>,
              but the stored context information is bound to all the assertions that
              follow the call to <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO_SCOPE</span></code>
              within the current scope.
            </li>
</ul></div>
<div class="tip"><table border="0" summary="Tip">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../../doc/src/images/tip.png"></td>
<th align="left">Tip</th>
</tr>
<tr><td align="left" valign="top"><p>
            Since Boost <a class="link" href="../../change_log.html#ref_CHANGE_LOG_3_10">Boost 1.70</a>, <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code> can accept multiple
            arguments.
          </p></td></tr>
</table></div>
<div class="tip"><table border="0" summary="Tip">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../../doc/src/images/tip.png"></td>
<th align="left">Tip</th>
</tr>
<tr><td align="left" valign="top"><p>
            <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO_SCOPE</span></code>
            has been introduced in <a class="link" href="../../change_log.html#ref_CHANGE_LOG_3_10">Boost 1.70</a>.
          </p></td></tr>
</table></div>
<h6>
<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h3"></a>
          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.example_descr0"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.example_descr0">Example:
          Scope-bound context</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example81</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">void</span> <span class="identifier">test</span><span class="special">()</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="number">2</span> <span class="special">!=</span> <span class="number">2</span><span class="special">);</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test_case1</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST_CONTEXT</span><span class="special">(</span><span class="string">"Alpha"</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="number">1</span> <span class="special">!=</span> <span class="number">1</span><span class="special">);</span>
    <span class="identifier">test</span><span class="special">();</span>

    <span class="identifier">BOOST_TEST_CONTEXT</span><span class="special">(</span><span class="string">"Be"</span> <span class="special">&lt;&lt;</span> <span class="string">"ta"</span><span class="special">)</span>
      <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="number">3</span> <span class="special">!=</span> <span class="number">3</span><span class="special">);</span>

    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="number">4</span> <span class="special">==</span> <span class="number">4</span><span class="special">);</span>
  <span class="special">}</span>

  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="number">5</span> <span class="special">!=</span> <span class="number">5</span><span class="special">);</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span>
<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">20</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_case1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">1</span> <span class="special">!=</span> <span class="number">1</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">1</span> <span class="special">==</span> <span class="number">1</span><span class="special">]</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">Alpha</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">14</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_case1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">2</span> <span class="special">!=</span> <span class="number">2</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">2</span> <span class="special">==</span> <span class="number">2</span><span class="special">]</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">Alpha</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">24</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_case1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">3</span> <span class="special">!=</span> <span class="number">3</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">3</span> <span class="special">==</span> <span class="number">3</span><span class="special">]</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">Alpha</span>
    <span class="identifier">Beta</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">29</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_case1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">5</span> <span class="special">!=</span> <span class="number">5</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">5</span> <span class="special">==</span> <span class="number">5</span><span class="special">]</span>

<span class="special">***</span> <span class="number">4</span> <span class="identifier">failures</span> <span class="identifier">are</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example81"</span>
</pre>
                </td></tr></tbody>
</table></div>
<p>
          In the previous example, there is an opening brace right after <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code>: this pair of braces
          defines the scope in which the diagnostic message is in effect. If there
          is no braces, the scope applies only to the following statement. <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code> declarations can nest.
        </p>
<p>
          With <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code>,
          we can further improve our initial example, by putting variable <code class="computeroutput"><span class="identifier">level</span></code> into a scope-level context and
          not pass it as function parameter:
        </p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">Processor</span><span class="special">&amp;</span> <span class="identifier">processor</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">limit</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">limit</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">);</span>
    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">));</span>
    <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">++</span><span class="identifier">j</span><span class="special">)</span> <span class="special">{</span>
      <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">);</span>
      <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter j = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">j</span><span class="special">);</span>
      <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">));</span>
    <span class="special">}</span>
  <span class="special">}</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test1</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">Processor</span> <span class="identifier">processor</span><span class="special">;</span>

  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">level</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">3</span><span class="special">;</span> <span class="special">++</span><span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">BOOST_TEST_CONTEXT</span><span class="special">(</span><span class="string">"With optimization level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
      <span class="identifier">processor</span><span class="special">.</span><span class="identifier">optimization_level</span><span class="special">(</span><span class="identifier">level</span><span class="special">);</span>
      <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">processor</span><span class="special">,</span> <span class="number">2</span><span class="special">);</span>
    <span class="special">}</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
          If we observe that variable <code class="computeroutput"><span class="identifier">i</span></code>
          also applies in a certain scope, we can improve our example further still.
        </p>
<h6>
<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h4"></a>
          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.example_descr1"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.example_descr1">Example:
          Using contexts</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example82</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">struct</span> <span class="identifier">Processor</span>
<span class="special">{</span>
  <span class="keyword">int</span> <span class="identifier">level</span><span class="special">;</span>
  <span class="keyword">void</span> <span class="identifier">optimization_level</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">l</span><span class="special">)</span> <span class="special">{</span> <span class="identifier">level</span> <span class="special">=</span> <span class="identifier">l</span><span class="special">;</span> <span class="special">}</span>
  <span class="keyword">bool</span> <span class="identifier">op1</span><span class="special">(</span><span class="keyword">int</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">2</span><span class="special">;</span> <span class="special">}</span>
  <span class="keyword">bool</span> <span class="identifier">op2</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">int</span><span class="special">)</span> <span class="special">{</span> <span class="keyword">return</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">1</span><span class="special">;</span> <span class="special">}</span>
<span class="special">};</span>

<span class="keyword">void</span> <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">Processor</span><span class="special">&amp;</span> <span class="identifier">processor</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">limit</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">limit</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">BOOST_TEST_CONTEXT</span><span class="special">(</span><span class="string">"With parameter i = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">)</span> <span class="special">{</span>
      <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">));</span>
      <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">++</span><span class="identifier">j</span><span class="special">)</span> <span class="special">{</span>
        <span class="identifier">BOOST_TEST_INFO</span><span class="special">(</span><span class="string">"With parameter j = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">j</span><span class="special">);</span>
        <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">));</span>
      <span class="special">}</span>
    <span class="special">}</span>
  <span class="special">}</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test1</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">Processor</span> <span class="identifier">processor</span><span class="special">;</span>

  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">level</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">3</span><span class="special">;</span> <span class="special">++</span><span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
    <span class="identifier">BOOST_TEST_CONTEXT</span><span class="special">(</span><span class="string">"With optimization level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
      <span class="identifier">processor</span><span class="special">.</span><span class="identifier">optimization_level</span><span class="special">(</span><span class="identifier">level</span><span class="special">);</span>
      <span class="identifier">test_operations</span><span class="special">(</span><span class="identifier">processor</span><span class="special">,</span> <span class="number">2</span><span class="special">);</span>
    <span class="special">}</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span>
<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">27</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">)</span> <span class="identifier">has</span> <span class="identifier">failed</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">With</span> <span class="identifier">optimization</span> <span class="identifier">level</span> <span class="number">1</span>
    <span class="identifier">With</span> <span class="identifier">parameter</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span>
    <span class="identifier">With</span> <span class="identifier">parameter</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">24</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">)</span> <span class="identifier">has</span> <span class="identifier">failed</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">With</span> <span class="identifier">optimization</span> <span class="identifier">level</span> <span class="number">2</span>
    <span class="identifier">With</span> <span class="identifier">parameter</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">24</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">processor</span><span class="special">.</span><span class="identifier">op1</span><span class="special">(</span><span class="identifier">i</span><span class="special">)</span> <span class="identifier">has</span> <span class="identifier">failed</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">With</span> <span class="identifier">optimization</span> <span class="identifier">level</span> <span class="number">2</span>
    <span class="identifier">With</span> <span class="identifier">parameter</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">27</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">processor</span><span class="special">.</span><span class="identifier">op2</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">j</span><span class="special">)</span> <span class="identifier">has</span> <span class="identifier">failed</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">With</span> <span class="identifier">optimization</span> <span class="identifier">level</span> <span class="number">2</span>
    <span class="identifier">With</span> <span class="identifier">parameter</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span>
    <span class="identifier">With</span> <span class="identifier">parameter</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">0</span>

<span class="special">***</span> <span class="number">4</span> <span class="identifier">failures</span> <span class="identifier">are</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example82"</span>
</pre>
                </td></tr></tbody>
</table></div>
<p>
          Finally, it is possible to pass several arguments to <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code>,
          which is more convenient than having several scopes:
        </p>
<h6>
<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h5"></a>
          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.example_descr2"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.example_descr2">Example:
          Multiple arguments to <code class="computeroutput"><span class="identifier">BOOST_TEST_CONTEXT</span></code></a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example83</span> <span class="identifier">multicontext</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cmath</span><span class="special">&gt;</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test_multi_context</span><span class="special">)</span>
<span class="special">{</span>
  <span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">level</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="number">10</span><span class="special">;</span> <span class="special">++</span><span class="identifier">level</span><span class="special">)</span> <span class="special">{</span>
    <span class="keyword">int</span> <span class="identifier">rand_value</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">abs</span><span class="special">(</span><span class="identifier">rand</span><span class="special">())</span> <span class="special">%</span> <span class="number">50</span><span class="special">;</span>
    <span class="identifier">BOOST_TEST_CONTEXT</span><span class="special">(</span><span class="string">"With level "</span> <span class="special">&lt;&lt;</span> <span class="identifier">level</span><span class="special">,</span> <span class="string">"Random value="</span> <span class="special">&lt;&lt;</span> <span class="identifier">rand_value</span><span class="special">){</span>
      <span class="keyword">for</span><span class="special">(</span> <span class="keyword">int</span> <span class="identifier">j</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> <span class="identifier">j</span> <span class="special">&lt;</span> <span class="identifier">rand_value</span><span class="special">;</span> <span class="identifier">j</span><span class="special">++)</span> <span class="special">{</span>
        <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">level</span> <span class="special">&lt;</span> <span class="identifier">rand_value</span><span class="special">);</span>
        <span class="identifier">rand_value</span> <span class="special">/=</span> <span class="number">2</span><span class="special">;</span>
      <span class="special">}</span>
    <span class="special">}</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span>
<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">19</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_multi_context"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="identifier">rand_value</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">7</span> <span class="special">&gt;=</span> <span class="number">5</span><span class="special">]</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">With</span> <span class="identifier">level</span> <span class="number">7</span>
    <span class="identifier">Random</span> <span class="identifier">value</span><span class="special">=</span><span class="number">42</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">19</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_multi_context"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="identifier">rand_value</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">8</span> <span class="special">&gt;=</span> <span class="number">6</span><span class="special">]</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">With</span> <span class="identifier">level</span> <span class="number">8</span>
    <span class="identifier">Random</span> <span class="identifier">value</span><span class="special">=</span><span class="number">49</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">19</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test_multi_context"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">level</span> <span class="special">&lt;</span> <span class="identifier">rand_value</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">9</span> <span class="special">&gt;=</span> <span class="number">5</span><span class="special">]</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">With</span> <span class="identifier">level</span> <span class="number">9</span>
    <span class="identifier">Random</span> <span class="identifier">value</span><span class="special">=</span><span class="number">21</span>

<span class="special">***</span> <span class="number">3</span> <span class="identifier">failures</span> <span class="identifier">are</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example83 multicontext"</span>
</pre>
                </td></tr></tbody>
</table></div>
<p>
          <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO_SCOPE</span></code> is
          convenient when you augment the current scope information as new information
          arrives. The following example calls several time a quadratic polynomial
          estimation function with random polynomial. As the random values are drawn
          in a loop, they are placed in the current scope with <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO_SCOPE</span></code>,
          which allows us to easily debug the function.
        </p>
<h6>
<a name="boost_test.test_output.test_tools_support_for_logging.contexts.h6"></a>
          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.contexts.example_descr3"></a></span><a class="link" href="contexts.html#boost_test.test_output.test_tools_support_for_logging.contexts.example_descr3">Example:
          Sticky context with <code class="computeroutput"><span class="identifier">BOOST_TEST_INFO_SCOPE</span></code></a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example84</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">random</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">cmath</span><span class="special">&gt;</span>

<span class="comment">// this function does not compute properly the polynomial root estimation</span>
<span class="comment">// in the case of a double root.</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">random_generator_t</span><span class="special">&gt;</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">estimate_polynomial_roots</span><span class="special">(</span>
  <span class="identifier">random_generator_t</span><span class="special">&amp;</span> <span class="identifier">gen</span><span class="special">,</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">function</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">(</span><span class="keyword">double</span><span class="special">)&gt;</span> <span class="identifier">polynomial</span><span class="special">)</span> <span class="special">{</span>

  <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">std</span><span class="special">;</span>

  <span class="identifier">std</span><span class="special">::</span><span class="identifier">uniform_real_distribution</span><span class="special">&lt;&gt;</span> <span class="identifier">dis</span><span class="special">(-</span><span class="number">10</span><span class="special">,</span> <span class="number">10</span><span class="special">);</span>
  <span class="keyword">double</span> <span class="identifier">x1</span> <span class="special">=</span> <span class="identifier">dis</span><span class="special">(</span><span class="identifier">gen</span><span class="special">);</span>
  <span class="keyword">double</span> <span class="identifier">x2</span> <span class="special">=</span> <span class="identifier">dis</span><span class="special">(</span><span class="identifier">gen</span><span class="special">);</span>
  <span class="keyword">double</span> <span class="identifier">fx1</span> <span class="special">=</span> <span class="identifier">polynomial</span><span class="special">(</span><span class="identifier">x1</span><span class="special">);</span>
  <span class="keyword">double</span> <span class="identifier">fx2</span> <span class="special">=</span> <span class="identifier">polynomial</span><span class="special">(</span><span class="identifier">x2</span><span class="special">);</span>

  <span class="identifier">BOOST_TEST_INFO_SCOPE</span><span class="special">(</span><span class="string">"sample1 = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">x1</span><span class="special">);</span>
  <span class="identifier">BOOST_TEST_INFO_SCOPE</span><span class="special">(</span><span class="string">"sample2 = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">x2</span><span class="special">);</span>

  <span class="comment">// from Vieta formula</span>
  <span class="keyword">double</span> <span class="identifier">minus_b</span> <span class="special">=</span> <span class="identifier">x2</span> <span class="special">+</span> <span class="identifier">x1</span> <span class="special">-</span> <span class="special">(</span><span class="identifier">fx2</span> <span class="special">-</span> <span class="identifier">fx1</span><span class="special">)</span> <span class="special">/</span> <span class="special">(</span><span class="identifier">x2</span> <span class="special">-</span> <span class="identifier">x1</span><span class="special">);</span>
  <span class="keyword">double</span> <span class="identifier">c</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">x1</span> <span class="special">*</span> <span class="identifier">fx2</span> <span class="special">-</span> <span class="identifier">x2</span> <span class="special">*</span> <span class="identifier">fx1</span> <span class="special">+</span> <span class="identifier">x2</span> <span class="special">*</span> <span class="identifier">x1</span> <span class="special">*</span> <span class="identifier">x1</span> <span class="special">-</span> <span class="identifier">x1</span> <span class="special">*</span> <span class="identifier">x2</span> <span class="special">*</span> <span class="identifier">x2</span><span class="special">)</span> <span class="special">/</span> <span class="special">(</span><span class="identifier">x1</span> <span class="special">-</span> <span class="identifier">x2</span><span class="special">);</span>

  <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">minus_b</span> <span class="special">*</span> <span class="identifier">minus_b</span> <span class="special">&gt;=</span> <span class="number">4</span><span class="special">*</span><span class="identifier">c</span><span class="special">);</span>

  <span class="keyword">return</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">make_pair</span><span class="special">(</span>
    <span class="special">(</span><span class="identifier">minus_b</span> <span class="special">-</span> <span class="identifier">sqrt</span><span class="special">(</span><span class="identifier">minus_b</span> <span class="special">*</span> <span class="identifier">minus_b</span> <span class="special">-</span> <span class="number">4</span> <span class="special">*</span> <span class="identifier">c</span><span class="special">))</span> <span class="special">/</span> <span class="number">2</span><span class="special">,</span>
    <span class="special">(</span><span class="identifier">minus_b</span> <span class="special">+</span> <span class="identifier">sqrt</span><span class="special">(</span><span class="identifier">minus_b</span> <span class="special">*</span> <span class="identifier">minus_b</span> <span class="special">-</span> <span class="number">4</span> <span class="special">*</span> <span class="identifier">c</span><span class="special">))</span> <span class="special">/</span> <span class="number">2</span><span class="special">);</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">quadratic_estimation</span><span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">random_device</span> <span class="identifier">rd</span><span class="special">;</span>
  <span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="identifier">seed</span> <span class="special">=</span> <span class="identifier">rd</span><span class="special">();</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">mt19937</span> <span class="identifier">gen</span><span class="special">(</span><span class="identifier">seed</span><span class="special">);</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">uniform_int_distribution</span><span class="special">&lt;&gt;</span> <span class="identifier">dis</span><span class="special">(-</span><span class="number">10</span><span class="special">,</span> <span class="number">10</span><span class="special">);</span>

  <span class="identifier">BOOST_TEST_MESSAGE</span><span class="special">(</span><span class="string">"Seed = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">seed</span><span class="special">);</span>

  <span class="keyword">for</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">50</span><span class="special">;</span> <span class="identifier">i</span><span class="special">++)</span> <span class="special">{</span>
    <span class="identifier">BOOST_TEST_INFO_SCOPE</span><span class="special">(</span><span class="string">"trial "</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span><span class="special">+</span><span class="number">1</span><span class="special">);</span>
    <span class="keyword">int</span> <span class="identifier">root1</span> <span class="special">=</span> <span class="identifier">dis</span><span class="special">(</span><span class="identifier">gen</span><span class="special">);</span>
    <span class="keyword">int</span> <span class="identifier">root2</span> <span class="special">=</span> <span class="identifier">dis</span><span class="special">(</span><span class="identifier">gen</span><span class="special">);</span>
    <span class="keyword">if</span><span class="special">(</span><span class="identifier">root1</span> <span class="special">&gt;</span> <span class="identifier">root2</span><span class="special">)</span> <span class="special">{</span>
      <span class="identifier">std</span><span class="special">::</span><span class="identifier">swap</span><span class="special">(</span><span class="identifier">root1</span><span class="special">,</span> <span class="identifier">root2</span><span class="special">);</span>
    <span class="special">}</span>
    <span class="identifier">BOOST_TEST_INFO_SCOPE</span><span class="special">(</span><span class="string">"root1 = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">root1</span><span class="special">);</span>
    <span class="identifier">BOOST_TEST_INFO_SCOPE</span><span class="special">(</span><span class="string">"root2 = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">root2</span><span class="special">);</span>

    <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">estimated</span> <span class="special">=</span> <span class="identifier">estimate_polynomial_roots</span><span class="special">(</span>
      <span class="identifier">gen</span><span class="special">,</span>
      <span class="special">[</span><span class="identifier">root1</span><span class="special">,</span> <span class="identifier">root2</span><span class="special">](</span><span class="keyword">double</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">-&gt;</span> <span class="keyword">double</span> <span class="special">{</span> <span class="keyword">return</span> <span class="special">(</span><span class="identifier">x</span> <span class="special">-</span> <span class="identifier">root1</span><span class="special">)</span> <span class="special">*</span> <span class="special">(</span><span class="identifier">x</span> <span class="special">-</span> <span class="identifier">root2</span><span class="special">);</span> <span class="special">});</span>

    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">estimated</span><span class="special">.</span><span class="identifier">first</span> <span class="special">==</span> <span class="keyword">double</span><span class="special">(</span><span class="identifier">root1</span><span class="special">),</span> <span class="number">10.</span> <span class="special">%</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">test_tools</span><span class="special">::</span><span class="identifier">tolerance</span><span class="special">());</span>
    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">estimated</span><span class="special">.</span><span class="identifier">second</span> <span class="special">==</span> <span class="keyword">double</span><span class="special">(</span><span class="identifier">root2</span><span class="special">),</span> <span class="number">10.</span> <span class="special">%</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">test_tools</span><span class="special">::</span><span class="identifier">tolerance</span><span class="special">());</span>
  <span class="special">}</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">example</span> <span class="special">--</span><span class="identifier">log_level</span><span class="special">=</span><span class="identifier">message</span>
<span class="identifier">Seed</span> <span class="special">=</span> <span class="number">162981956</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">34</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"quadratic_estimation"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">minus_b</span> <span class="special">*</span> <span class="identifier">minus_b</span> <span class="special">&gt;=</span> <span class="number">4</span><span class="special">*</span><span class="identifier">c</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[-</span><span class="number">13.999999999999998</span> <span class="special">*</span> <span class="special">-</span><span class="number">13.999999999999998</span> <span class="special">&lt;</span> <span class="number">195.99999999999997</span><span class="special">]</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">trial</span> <span class="number">14</span>
    <span class="identifier">root1</span> <span class="special">=</span> <span class="special">-</span><span class="number">7</span>
    <span class="identifier">root2</span> <span class="special">=</span> <span class="special">-</span><span class="number">7</span>
    <span class="identifier">sample1</span> <span class="special">=</span> <span class="number">4.66289</span>
    <span class="identifier">sample2</span> <span class="special">=</span> <span class="number">1.70234</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">64</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"quadratic_estimation"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">estimated</span><span class="special">.</span><span class="identifier">first</span> <span class="special">==</span> <span class="keyword">double</span><span class="special">(</span><span class="identifier">root1</span><span class="special">)</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[-</span><span class="identifier">nan</span> <span class="special">!=</span> <span class="special">-</span><span class="number">7</span><span class="special">].</span> <span class="identifier">Relative</span> <span class="identifier">difference</span> <span class="identifier">exceeds</span> <span class="identifier">tolerance</span> <span class="special">[-</span><span class="identifier">nan</span> <span class="special">&gt;</span> <span class="number">0.1</span><span class="special">]</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">trial</span> <span class="number">14</span>
    <span class="identifier">root1</span> <span class="special">=</span> <span class="special">-</span><span class="number">7</span>
    <span class="identifier">root2</span> <span class="special">=</span> <span class="special">-</span><span class="number">7</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">65</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"quadratic_estimation"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">estimated</span><span class="special">.</span><span class="identifier">second</span> <span class="special">==</span> <span class="keyword">double</span><span class="special">(</span><span class="identifier">root2</span><span class="special">)</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[-</span><span class="identifier">nan</span> <span class="special">!=</span> <span class="special">-</span><span class="number">7</span><span class="special">].</span> <span class="identifier">Relative</span> <span class="identifier">difference</span> <span class="identifier">exceeds</span> <span class="identifier">tolerance</span> <span class="special">[-</span><span class="identifier">nan</span> <span class="special">&gt;</span> <span class="number">0.1</span><span class="special">]</span>
<span class="identifier">Failure</span> <span class="identifier">occurred</span> <span class="identifier">in</span> <span class="identifier">a</span> <span class="identifier">following</span> <span class="identifier">context</span><span class="special">:</span>
    <span class="identifier">trial</span> <span class="number">14</span>
    <span class="identifier">root1</span> <span class="special">=</span> <span class="special">-</span><span class="number">7</span>
    <span class="identifier">root2</span> <span class="special">=</span> <span class="special">-</span><span class="number">7</span>

<span class="special">***</span> <span class="number">3</span> <span class="identifier">failures</span> <span class="identifier">are</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"example84"</span>
</pre>
                </td></tr></tbody>
</table></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2022 Boost.Test contributors<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="checkpoints.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../test_tools_support_for_logging.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="log_floating_points.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
